package cn.bugDance.domain.activity.service.partake;

import cn.bugDance.domain.activity.model.aggregate.ActivityPartakeOrderAggregate;
import cn.bugDance.domain.activity.model.entity.*;

import cn.bugDance.domain.activity.model.valobj.ActivityPartakeOrderStateVO;
import cn.bugDance.domain.activity.repository.IActivityRepository;
import cn.bugDance.types.enums.ResponseCode;
import cn.bugDance.types.exception.AppException;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.RandomStringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;

@Service
@Slf4j
public class RaffleActivityPartakeService extends AbstractRaffleActivityPartakeService{

    @Resource
    private IActivityRepository activityRepository;

    private final SimpleDateFormat monthFormat = new SimpleDateFormat("yyyy-MM");
    private final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");

    @Override
    protected ActivityPartakeOrderEntity createActivityPartakeOrder(PartakeActivityFactorEntity partakeActivityFactorEntity, Date nowDate) {
        String userId= partakeActivityFactorEntity.getUserId();
        Long activityId = partakeActivityFactorEntity.getActivityId();

        String month = monthFormat.format(nowDate);
        String day = dateFormat.format(nowDate);

        ActivityAccountEntity activityAccountEntity = activityRepository.queryActivityAccountByUserIdAndActivityId(userId, activityId);
        if(null == activityAccountEntity || activityAccountEntity.getTotalCountSurplus() <= 0){
            log.info("【活动服务层-RaffleActivityPartakeService】: 用户活动账户总账额为空或者用户无活动账户, userId: {}, activityId: {}", userId, activityId);
            throw new AppException(ResponseCode.ACTIVITY_ACCOUNT_TOTAL_COUNT_ERROR.getCode(), ResponseCode.ACTIVITY_ACCOUNT_TOTAL_COUNT_ERROR.getInfo());
        }

        ActivityMonthAccountEntity activityMonthAccountEntity = activityRepository.queryActivityMonthAccountEntity(userId, activityId, month);
        if(null != activityMonthAccountEntity && activityMonthAccountEntity.getMonthCountSurplus() <= 0){
            log.info("【活动服务层-RaffleActivityPartakeService】: 用户活动账户月账额为空或者用户无活动账户, userId: {}, activityId: {}", userId, activityId);
            throw new AppException(ResponseCode.ACTIVITY_ACCOUNT_MONTH_COUNT_ERROR.getCode(), ResponseCode.ACTIVITY_ACCOUNT_MONTH_COUNT_ERROR.getInfo());
        }

        ActivityDayAccountEntity activityDayAccountEntity = activityRepository.queryActivityDayAccountEntity(userId, activityId, day);
        if(null != activityDayAccountEntity && activityDayAccountEntity.getDayCountSurplus() <= 0){
            log.info("【活动服务层-RaffleActivityPartakeService】: 用户活动账户日账额为空或者用户无活动账户, userId: {}, activityId: {}", userId, activityId);
            throw new AppException(ResponseCode.ACTIVITY_ACCOUNT_DAY_COUNT_ERROR.getCode(), ResponseCode.ACTIVITY_ACCOUNT_DAY_COUNT_ERROR.getInfo());
        }

        boolean isExistMonthAccount = activityMonthAccountEntity != null;
        boolean isExistDayAccount = activityDayAccountEntity != null;

        if(!isExistMonthAccount){
            activityMonthAccountEntity = new ActivityMonthAccountEntity();
            activityMonthAccountEntity.setUserId(userId);
            activityMonthAccountEntity.setActivityId(activityId);
            activityMonthAccountEntity.setMonth(month);
            activityMonthAccountEntity.setMonthCount(activityAccountEntity.getMonthCount());
            activityMonthAccountEntity.setMonthCountSurplus(activityAccountEntity.getMonthCount());
        }
        if(!isExistDayAccount){
            activityDayAccountEntity = new ActivityDayAccountEntity();
            activityDayAccountEntity.setUserId(userId);
            activityDayAccountEntity.setActivityId(activityId);
            activityDayAccountEntity.setDay(day);
            activityDayAccountEntity.setDayCount(activityAccountEntity.getDayCount());
            activityDayAccountEntity.setDayCountSurplus(activityAccountEntity.getDayCount());
        }

        ActivityPartakeOrderEntity activityPartakeOrderEntity = new ActivityPartakeOrderEntity();
        activityPartakeOrderEntity.setUserId(userId);
        activityPartakeOrderEntity.setActivityId(activityId);
        activityPartakeOrderEntity.setOrderId(RandomStringUtils.randomNumeric(12));
        activityPartakeOrderEntity.setOrderTime(nowDate);
        activityPartakeOrderEntity.setState(ActivityPartakeOrderStateVO.NOT_USED);

        ActivityPartakeOrderAggregate activityPartakeOrderAggregate = new ActivityPartakeOrderAggregate();
        activityPartakeOrderAggregate.setUserId(userId);
        activityPartakeOrderAggregate.setActivityId(activityId);
        activityPartakeOrderAggregate.setIsExistActivityMonthAccount(isExistMonthAccount);
        activityPartakeOrderAggregate.setIsExistActivityDayAccount(isExistDayAccount);
        activityPartakeOrderAggregate.setActivityPartakeOrderEntity(activityPartakeOrderEntity);
        activityPartakeOrderAggregate.setActivityMonthAccountEntity(activityMonthAccountEntity);
        activityPartakeOrderAggregate.setActivityDayAccountEntity(activityDayAccountEntity);
        activityPartakeOrderAggregate.setMonth(month);
        activityPartakeOrderAggregate.setDay(day);

        activityRepository.saveActivityPartakeOrder(activityPartakeOrderAggregate);

        return activityPartakeOrderEntity;
    }
}
